package com.itheima.tlias84.controller;

import com.itheima.tlias84.dto.SelectClazzDTO;
import com.itheima.tlias84.po.InsterClazzParam;
import com.itheima.tlias84.po.SelectClazzPageParam;
import com.itheima.tlias84.po.UpdateClazzParam;
import com.itheima.tlias84.service.ClazzService;
import com.itheima.tlias84.utils.ResultUtil;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/clazzs")
public class ClazzController {

    private static final Logger log = LoggerFactory.getLogger(ClazzController.class);
    @Resource
    ClazzService clazzService;
    @GetMapping
    public ResultUtil selectClazzByPage(SelectClazzPageParam param){
        if (Objects.isNull(param.getPage())||Objects.isNull(param.getPageSize())){
            return ResultUtil.fail("分页参数不能为空");
        }
        return clazzService.selectClazzByPage(param);
    }
    @DeleteMapping("/{id}")
    public ResultUtil deleteById(@PathVariable("id") Integer id){
        log.info("删除id{}",id);
        int i = clazzService.deleteById(id);
        return i == 1 ? ResultUtil.success() : ResultUtil.fail();
    }
    @PostMapping
    public ResultUtil insertClazz(@Validated @RequestBody InsterClazzParam param){
        log.info("新增班级信息：{}",param);
        int i = clazzService.insertClazz(param);
        return i == 1 ? ResultUtil.success() : ResultUtil.fail();
    }
    @GetMapping("/{id}")
    public ResultUtil selectClazzById(@PathVariable("id") Integer id){
        SelectClazzDTO dto = clazzService.selectClazzById(id);
        return ResultUtil.success(dto);
    }
    @PutMapping
    public ResultUtil updateClazz(@RequestBody UpdateClazzParam param){
        log.info("修改班级信息：{}",param);
        int i = clazzService.updateClazz(param);
        return i == 1 ? ResultUtil.success() : ResultUtil.fail();
    }
    @GetMapping("/list")
    public ResultUtil selectAll(){
        List<SelectClazzDTO> dtos = clazzService.selectAll();
        return ResultUtil.success(dtos);
    }
}
